var CaseSelection = {
    id: "caseSelectionTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

CaseSelection.initColumn = function () {
    var columns =
        [
            {field: 'selectItem', radio: true},
            {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {
                title: '状态', field: 'selectionStatus', align: 'center', valign: 'middle', width: 100, sortable: true,
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return "待评选";
                    } else {
                        return "已评选"
                    }
                }
            },
            {title: '病例类型', field: 'caseType', align: 'center', valign: 'middle', width: 110, sortable: true},
            {
                title: '姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 110,
                formatter: function (value, row, index) {
                    if(row.customerAge == "" || row.customerAge == null || row.customerAge == "null"){
                        return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                    } else if (row.customerAge >= 0 && row.customerAge < 18) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/nan-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/nv-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    } else if (row.customerAge >= 18 && row.customerAge <= 50) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/male-adult.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/lfemale-child.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    } else if (row.customerAge > 50) {
                        if (row.customerSex == 1) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/male-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else if (row.customerSex == 2) {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/female-old.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        } else {
                            return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'CaseSelection.seeOpenBodyPulpCase(" + '"' + row.id + '"'+','+'"' + row.selectionStatus + '"' + ")'><img src='" + Feng.ctxPath + "/static/img/unkown-sex.png' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                        }
                    }
                }
            },
            {
                title: '性别', field: 'customerSex', align: 'center', valign: 'middle', width: 80, sortable: true,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return "男";
                    } else if (value == 2) {
                        return "女"
                    } else {
                        return "未知"
                    }
                }
            },
            {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80},
            {
                title: '申请时间', field: 'applyTime', align: 'center', valign: 'middle', sortable: true, width: 150,
                formatter: function (value, row, index) {
                    return value.substring(0, 19);
                }
            },
            {
                title: '评选时间', field: 'selectionTime', align: 'center', valign: 'middle', sortable: true, width: 170,
                formatter: function (value, row, index) {
                    return value.substring(0, 19);
                }
            },
            {title: '评分', field: 'caseScore', align: 'center', valign: 'middle', sortable: true, width: 80},
            {
                title: '评语', field: 'caseComment', align: 'center', valign: 'middle', sortable: true, width: 200,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>'
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            },
            {
                title: '标签', field: 'mark', align: 'center', valign: 'middle', sortable: true, width: 200,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>'
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            },
            {
                title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 100,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>'
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            },
            {
                title: '操作', field: '', align: 'center', valign: 'middle', width: 60,
                formatter: function (value, row, index) {
                    if (row.selectionStatus == 0) {
                        return '<button title="查看病例" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="CaseSelection.seeOpenBodyPulpCase(\'' + row.id + '\',\'' + row.selectionStatus + '\')" type="button" class="btn"><span class="glyphicon glyphicon-pencil"></span></button>'
                    } else {
                        return '<button title="查看病例" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="CaseSelection.seeOpenBodyPulpCase(\'' + row.id + '\',\'' + row.selectionStatus + '\')" type="button" class="btn"><span class="glyphicon glyphicon-eye-open"></span></button>'
                    }
                }
            }
        ];
    return columns;
};

/**
 * 打分已提交牙体牙髓病例
 * @param id
 */
CaseSelection.seeOpenBodyPulpCase = function (id,status) {
    if (status == 0) {
        var index = layer.open({
            type: 2,
            title: '牙体牙髓病例打分',
            area: ['100%', '100%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/caseSelectionUpload/to_body_pulp_case_score/' + id
        });
        this.layerIndex = index;
    } else {
        var index = layer.open({
            type: 2,
            title: '查看牙体牙髓病例',
            area: ['100%', '100%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/caseSelectionUpload/to_body_pulp_case_open/' + id
        });
        this.layerIndex = index;
    }
};

/**
 * 打分/查看已提交牙体牙髓病例
 * @param id
 */
CaseSelection.seeBodyPulpCase = function (id) {
    var index = layer.open({
        type: 2,
        title: '查看牙体牙髓病例',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/caseSelectionUpload/to_body_pulp_case_score/' + id
    });
    this.layerIndex = index;
};

/**
 * 打分已提交牙体牙髓病例
 * @param id
 */
CaseSelection.openBodyPulpCase = function (id) {
    var index = layer.open({
        type: 2,
        title: '查看牙体牙髓病例',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/caseSelectionUpload/to_body_pulp_case_open/' + id
    });
    this.layerIndex = index;
};

//关闭
CaseSelection.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};

/**
 * 查询预约客户列表
 */
CaseSelection.search = function () {
    CaseSelection.table.refresh({query: CaseSelection.formParams()});
    CaseSelection.total();
};

//统计人数
CaseSelection.total = function () {
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/caseSelection/caseSelectiontotal',
        data: CaseSelection.formParams(),
        dataType: "json",
        success: function (data) {
            $("#daip").html(data[0].countNum);
            $("#yip").html(data[1].countNum);
            $("#thjl").html(data[2].countNum);
        }
    });
};

/**
 * 新市场预约表单查询提交参数
 */
CaseSelection.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#caseSelectionPublic", parent.document).val();  //姓名   电话   微信  预约号
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['isSelection'] = $("#isSelection").val();
    return queryData;
};

/**
 * 把对象拼接成URL的键值对参数
 * param1=value1&param2=value2
 */
var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {paramStr += '&' + getSingleData(param, i);});
    return paramStr.substr(1);
};

var getSingleData = function (param, key) {return key + "=" + param[key];};
/**
 * 检查是否选中
 */
CaseSelection.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        CaseSelection.seItem = selected[0];
        return true;
    }
};

/**
 * 重置
 */
CaseSelection.resetSearch = function () {
    $("#caseSelectionPublic", parent.document).val("");
    $("#startTime").val("");
    $("#endTime").val("");
    CaseSelection.search();
};
/**
 * 主页搜索
 */
$("#caseSelectionSearchAll", parent.document).click(function () {CaseSelection.search();});
/**
 * 初始化赋值时间
 */
CaseSelection.time = function () {
    var date = new Date();
    var currentYear = date.getFullYear();
    var currentMonth = date.getMonth() + 1;
    var currentDay = date.getDate();
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    $("#startTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "00:00:00");
    $("#endTime").val(currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59");
};

var scollPostion = 0;
/**
 * 获取表格滚动到什么地方
 */
CaseSelection.getScrollPosition = function (){
    scollPostion = $('#CaseSelection').bootstrapTable('getScrollPosition');
};

CaseSelection.uploadSelection = function () {
    document.getElementById("isSelection").value = "0";
    $('#caseSelectionTable').bootstrapTable('hideColumn', 'selectionTime');
    $('#caseSelectionTable').bootstrapTable('hideColumn', 'caseScore');
    $('#caseSelectionTable').bootstrapTable('hideColumn', 'caseComment');
    $('#caseSelectionTable').bootstrapTable('showColumn', 'mark');
    CaseSelection.search();
    $("#selectionWaiting").addClass('actionClick').siblings().removeClass('actionClick');
};

CaseSelection.selectioned = function () {
    document.getElementById("isSelection").value = "1";
    $('#caseSelectionTable').bootstrapTable('hideColumn', 'mark');
    $('#caseSelectionTable').bootstrapTable('showColumn', 'selectionTime');
    $('#caseSelectionTable').bootstrapTable('showColumn', 'caseScore');
    $('#caseSelectionTable').bootstrapTable('showColumn', 'caseComment');
    CaseSelection.search();
};

CaseSelection.gobackrecrod = function () {
    document.getElementById("isSelection").value = "2";
    $('#caseSelectionTable').bootstrapTable('hideColumn', 'mark');
    $('#caseSelectionTable').bootstrapTable('hideColumn', 'selectionTime');
    $('#caseSelectionTable').bootstrapTable('hideColumn', 'caseScore');
    $('#caseSelectionTable').bootstrapTable('hideColumn', 'caseComment');
    CaseSelection.search();
};

/**
 * 指定表格滚动到什么地方
 */
CaseSelection.scrollTo = function (){
    $("#CaseSelection").on('load-success.bs.table',function(data){
        $('#CaseSelection').bootstrapTable('scrollTo', scollPostion);
    });
};

$(function () {
    var defaultColunms = CaseSelection.initColumn();
    var table = new BSTable(CaseSelection.id, "/caseSelection/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(CaseSelection.formParams());
    CaseSelection.table = table.init();
    CaseSelection.uploadSelection();
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {CaseSelection.search()});
    checkDate = function () {CaseSelection.search()};
    callbackTable = function () {CaseSelection.search()};
});